package com.mrjy.video.bo;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.mrjy.common.entity.VideoTipPayment;
import com.mrjy.common.entity.UserBalanceInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.maintain.dao.IUserBalanceInfoDao;
import com.mrjy.video.dao.IVideoTipPaymentDao;


@Service("courseVideoTipPaymentBo")
public class VideoTipPaymentBo implements IVideoTipPaymentBo{
	@Resource
	private IVideoTipPaymentDao courseVideoTipPaymentDao;
	
	@Resource
	private IUserBalanceInfoDao userBalanceInfoDao;

	

	@Override
	public Object txSubmitPayment(VideoTipPayment courseVideoTipPayment,UserInfo userInfo) {
		if (userInfo == null) {
			return "userInfo_error";
		}
		UserBalanceInfo userBalance=userBalanceInfoDao.queryByUserId(userInfo.getId());				
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date now = courseVideoTipPaymentDao.getTime();		
		courseVideoTipPayment.setUserId(userInfo.getId());
		VideoTipPayment existUnpaidPayment = courseVideoTipPaymentDao.queryUnpaidByUserId(courseVideoTipPayment);  
		//查询订单是否存在，是否要新建
		if (existUnpaidPayment == null) {
			String paymentCode = "tip_" + userInfo.getId() + sdf.format(now);
			courseVideoTipPayment.setName("视频打赏");	
			courseVideoTipPayment.setCode(paymentCode);
			courseVideoTipPayment.setOrderTime(now);	
			courseVideoTipPayment.setStatusId(1);
			int row = courseVideoTipPaymentDao.insert(courseVideoTipPayment);
			if (row != 1) {
				courseVideoTipPayment.setMessageString("update_error");
				return courseVideoTipPayment;
			}else{
				//支付金额与余额比较
				if(userBalance.getMoney()<courseVideoTipPayment.getMoney()){
					courseVideoTipPayment.setMessageString("balance_error");
					return courseVideoTipPayment;
				}
				//余额支付
				UserBalanceInfo balanceUpdate=new UserBalanceInfo();
				balanceUpdate.setId(userBalance.getId());
				balanceUpdate.setMoney(userBalance.getMoney()-courseVideoTipPayment.getMoney());
				int row2 = userBalanceInfoDao.updateById(balanceUpdate);
				if (row2 != 1) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();		
					courseVideoTipPayment.setMessageString("pay_error");
					return courseVideoTipPayment;
				}else{
					courseVideoTipPayment=courseVideoTipPaymentDao.queryUnpaidByUserId(courseVideoTipPayment);
					courseVideoTipPayment.setCompleteTime(now);
					courseVideoTipPayment.setStatusId(2);
					courseVideoTipPayment.setPaymentMethod("balance");
					courseVideoTipPaymentDao.updatePayment(courseVideoTipPayment);
				}
			}
			courseVideoTipPayment.setMessageString("ok");
			courseVideoTipPayment.setUrl("/course/courseVideoTip/getPaymentByCode.do?code▲" + courseVideoTipPayment.getCode());
			return courseVideoTipPayment;
		}else {
			existUnpaidPayment.setCode("tip_" + userInfo.getId() + sdf.format(now));			
			existUnpaidPayment.setOrderTime(now);
			existUnpaidPayment.setMoney(courseVideoTipPayment.getMoney());			
			int row = courseVideoTipPaymentDao.updatePayment(existUnpaidPayment);
			if (row != 1) {
				existUnpaidPayment.setMessageString("update_error");
				return existUnpaidPayment;
			}else{
				//支付金额与余额比较
				if(userBalance.getMoney()<courseVideoTipPayment.getMoney()){
					courseVideoTipPayment.setMessageString("balance_error");
					return courseVideoTipPayment;
				}		
				//余额支付
				UserBalanceInfo balanceUpdate=new UserBalanceInfo();
				balanceUpdate.setId(userBalance.getId());
				balanceUpdate.setMoney(userBalance.getMoney()-courseVideoTipPayment.getMoney());
				int row2 = userBalanceInfoDao.updateById(balanceUpdate);
				if (row2 != 1) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();		
					courseVideoTipPayment.setMessageString("pay_error");
					return courseVideoTipPayment;
				}else{
					existUnpaidPayment.setCompleteTime(now);
					existUnpaidPayment.setStatusId(2);
					existUnpaidPayment.setPaymentMethod("balance");
					courseVideoTipPaymentDao.updatePayment(existUnpaidPayment);
				}
			}
			existUnpaidPayment.setMessageString("ok");
			existUnpaidPayment.setUrl("/course/courseVideoTip/getPaymentByCode.do?code▲" + existUnpaidPayment.getCode());
			return existUnpaidPayment;
		}
	}


}
